from django.db.models.signals import pre_save
from django.dispatch import receiver

from .core.loader import loader
from .migrate import Migration, Migrate
from ..system.models import Menu


@receiver((pre_save,), sender=Menu)
def save_table(sender, instance=None, **kwargs):
    if instance.is_dynamic and not instance.is_catalog:
        loader.load_table(instance, True)
        Migration().to_make('dynamic')
        Migrate().handle(**{'app_label': 'dynamic', 'migration_name': False})
